﻿using System;
using System.IO;
using LatoolNet;

namespace NumericalSolution {
  class Poisson1DFVMain {

    static String outfile = "c:/nsworkspace/poisson1d_fv.csv";
    static double beta = 5.0;
    static int numberOfControlVolumes = 20;

    public static void Main() {

      Poisson1DFV sim = new Poisson1DFV();
      sim.NumberOfControlVolumes = numberOfControlVolumes;
      sim.Beta = beta;
      Matrix result = sim.Solve();

      StreamWriter writer = File.CreateText(outfile);
      writer.WriteLine("x" + "," + "exact" + "," + "numerical");
      for (int i = 0; i < numberOfControlVolumes; i++) {
        double x = sim.posx(i);
        double exact = u(x);
        double numerical = result[i, 0];
        
        writer.Write(x + ", ");
        writer.Write(exact + ", ");
        writer.WriteLine(numerical);
      }
      writer.Close();
    }

    static double u(double x) {
      return -0.5 * beta * x * x + (0.5 * beta + 1) * x;
    }

  }
}
